▩ JNDI(Java Naming and Directory Interface)의 이해 - JNDI이름은 대소문자를 구분합니다. - 클래스에 이름(별명)을 부여하고 원격으로 접속하여 클래스를 사용할 수 있는 기술입니다. - 객체를 찾지 못하면 "javax.naming.NameNotFoundException"을 발생합니다. - API참고 주소: http://java.sun.com/j2se/1.5.0/docs/api/ - javax.naming.* - 웹로직 서버가 부팅시에 JNDI 객체를 등록합니다.
1. 원리
Naming Server는 JNDI 이름과 매핑된 객체를 찾아서 return해 줍니다.
Client ---> Naming Server --┬-- JNDI Name A <---> Object 1 <Weblogic> ├-- JNDI Name B <---> Object 2 └-- JNDI Name C <---> Object 3
2. Naming Serivce 의 종류 - LDAP(Lightweight Directory Access Protocol) . 네트워크 디렉토리 서비스의 표준인 x.500을 위한 간단한 형태 제공, 미시간대
- DNS(Domain Name System) . 인터넷 네이밍 시스템, 컴퓨터 이름(도메인)을 IP주소로 변환 . 대규모 분산 데이터 베이스 서버
- NIS(Network Information System) . SUN에서 개발된 네트워크 네이밍 서비스
- CosNaming(Common Object Services) . 코바응용프로그램을 위한 네이밍 서비스 지원
- MS Active Directory - JNDI . SUN에서 개발된 자바기반 객체 등록 서비스 . JNDI SPI(Service Provider Interface) 제공 . JNDI 서버의 실제 구현 기능은 각 Application Server의 Vendor가 제공함
3. JNDI 구조
Java Application ↕ JNDI API <----------- SUN 제공 API ↕ Service Provider <----------- Weblogic Server JNDI SPI ↕ LDAP, NIS, DNS, JNDI <---------- Weblogic
4. JNDI를 이용한 Application의 개발 구조 - Object Serialization :객체 변형 기술 - Remote Method Invocation: 객체 전송 기술 - Java Naming and Directory Interface: 객체 이름 지정 기술
Client--> Serialization --> RMI --> App Server --> JNDI -┬-- EJB Component A --┬- DB 데이터 변형 통신 ├-- EJB Component B --┤ └-- EJB Component C --┘
5. Context & SubContext(환경, 문맥) - 하나의 어플리케이션이 실행되는 환경이며 모든 사용자에게 영향을 미침니다. - application 변수등을 사용하면 그 변수는 context영역에 저장되어 모든 사용자에게 공유된다. - Context: 바인딩된 객체에 대한 정보들이 저장되는 일종의 저장공간 마치 트리구조와 비슷 하다. - Context의 하위 Context(Folder)를 SubContext(Sub Folder)라고 부른다. - Naming Convention을 통해 Context를 구분한다. (\. /) - Namespace는 context, subcontext를 모두 합해서 부르는 말이다. - Initial Context를 통해 탐색할 시작 위치를 지정 및 초기화 한다. - 개발자의 편의를 위해 JNDI 서비스는 내부적으로 자동화되어 운영됩니다.
▩ JNDI (Java Naming And Directory Interface)의 실습 - 'C:\bea\weblogic81\server\lib\weblogic.jar' 파일을 eclipse 프로젝트에 추가합니다.
1. JNDI 네이밍 서비스에 객체 등록(재등록)하기 - 반드시 Weblogic 서버가 실행되고 있어야 한다.
>>>>> Bind.java package jndi;
import javax.naming.*; import java.util.Properties;
public class Bind { public static void main(String args[]){ try{ //JNDI 서버 설정 정보 Properties h = new Properties(); h.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory"); h.put(Context.PROVIDER_URL, "t3://172.16.6.1:7001"); //JNDI 서비스를 이용하기위해 초기화를 합니다. //JNDI 서버 연결 Context ctx = new InitialContext(h); StringBuffer sb = new StringBuffer(); sb.append("왕눈이가 본 영화\n"); sb.append("----------------\n"); sb.append("1. 마파도\n"); sb.append("2. 봄여름가을겨울\n"); sb.append("3. 바람의 전설\n"); sb.append("4. 포레스트 검프\n"); //JNDI 서버에 객체를 등록합니다. //ctx.bind("jnditest", sb); //JNDI서버에 재 등록 ctx.rebind("jnditest", sb); System.out.println("JNDI 네이밍 서비스에 등록하였습니다."); }catch(Exception ex){ System.out.println(ex); } // catch }// main }// class
2. JNDI 네이밍 서비스에 객체 사용하기
>>>>> Lookup.java package jndi;
import javax.naming.*; import java.util.Properties;
public class Lookup { public static void main(String args[]){ try{ Properties h = new Properties(); h.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory"); h.put(Context.PROVIDER_URL, "t3://172.16.6.1:7001"); Context ctx = new InitialContext(h); //JNDI서비스에 접속하여 객체를 찾습니다. Object obj = ctx.lookup("jnditest"); //원래의 객체 타입으로 형변환을 합니다. StringBuffer sb = (StringBuffer)obj; System.out.println("jnditest JNDI 이름으로 등록된 문자열 :"); System.out.println(sb.toString()); }catch(Exception ex){ System.out.println(ex); } // catch }// main }// class
3. JNDI 네이밍 서비스에 등록된 객체 삭제하기
>>>>> Unbind.java package jndi;
import javax.naming.*; import java.util.Properties;
public class Unbind { public static void main(String args[]){ try{ Properties h = new Properties(); h.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory"); h.put(Context.PROVIDER_URL, "t3://localhost:7001"); Context ctx = new InitialContext(h); ctx.unbind("jnditest"); System.out.println("jnditest JNDI 이름을 삭제하였습니다."); }catch(Exception ex){ System.out.println(ex); } // catch }// main }// class
[참고] JNDI 서비스 지원 서버의 위치 지정(많이 사용되지 않음) - 서버와 서버간 객체 참조시 이용됩니다.
1. JNDI 정보를 가지고 있는 파일을 작성합니다. >>>>> jndi.properties을 작성해 "C:\j2sdk1.4.2_09\jre\lib", "C:\Program Files\Java\j2re1.4.2_09\lib" 폴더에 저장한다. java.naming.factory.initial=weblogic.jndi.WLInitialContextFactory java.naming.provider.url=t3://localhost:7001
2. JNDI 서비스에 등록 >>>>> Bind.java package jndi;
import javax.naming.*; import java.util.Properties;
public class Bind { public static void main(String args[]){ try{ Context ctx = new InitialContext(); ctx.bind("jnditest", "☆★☆★☆★"); System.out.println("JNDI 네이밍 서비스에 등록하였습니다."); }catch(Exception ex){ System.out.println(ex); } // catch }// main }// class
3. JNDI 서비스 찾기 >>>>> Lookup.java package jndi;
import javax.naming.*; import java.util.Properties;
public class Lookup { public static void main(String args[]){ try{ Context ctx = new InitialContext(); //JNDI서비스에 접속하여 객체를 찾습니다. Object obj = ctx.lookup("jnditest"); //원래의 객체 타입으로 형변환을 합니다. String str = (String)obj; System.out.println("jnditest JNDI 이름으로 등록된 문자열 :" + str); }catch(Exception ex){ System.out.println(ex); } // catch }// main }// class
|